#include<iostream>
#include<cstdio>
using namespace std;
int h[100001],n;
void siftdown(int i){
	int flag=0;
	int t=i;
	while(i*2<=n&&flag==0){
		if(h[i]>h[i*2])t=i*2;
		if(i*2+1<=n&&h[t]>h[i*2+1])t=i*2+1;
		if(t!=i){
			swap(h[i],h[t]);
			i=t;
		}else flag=1;	
	} 
}
void creat(){
	for(int i=n/2;i>=1;i--)siftdown(i);
}
int dt(){
	int t=h[1];
	h[1]=h[n--];
	siftdown(1);
	return t;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>h[i];
	creat();
	while(n)printf("%d ",dt());
}
